<?php

// 退款退货管理

namespace app\client\controller;

use think\Db;
use think\Url;
use think\Session;
use think\Request;
use think\Controller;
use app\client\model\MemberRefund as MemberRefundModel;
use app\shop\model\MemberOrderAttached as MemberOrderAttachedModel;

class MemberRefund extends Controller
{

    /*
     *退款退货列表
     */
    public function index()
    {
        $inquire = Request::instance()->param();

        // 搜索条件
        $condition['shop_id'] = ['eq', Session::get('shop_id')];
        $condition['status'] = ['in', '6,7,9,10,11'];

        // 条件
        if (!empty($inquire['status'])) $condition['status'] = $inquire['status'];
        if (!empty($inquire['title'])) $condition['shop_order_number'] = $inquire['title'];

        if (!empty($inquire['interval_timer'])) {

            $interval_timer = explode(' / ', $inquire['interval_timer']);
            $condition['create_time'] = ['between time', [strtotime($interval_timer[0]), strtotime($interval_timer[1])]];

        }

        $data = Db::view('member_order', 'id,shop_order_number,shop_id,member_id,create_time,price,way,status')
            ->view('shop', 'member_id,title', 'shop.member_id = member_order.shop_id')
            ->view('member', 'id as user_id,username', 'member.id = member_order.member_id')
            ->where($condition)
            ->order(['create_time' => 'desc'])
            ->paginate(10, false, ['query' => $inquire]);


        return $this->fetch('index', [
            'mate_title' => '订单列表',
            'data'       => $data,
        ]);
    }


    /*
     * 订单审核
     */
    public function examine($id)
    {
        if (Request::instance()->isPost()) {

            $data = Request::instance()->param();
            if(!isset($data['order_status'])) fail('订单已经提交过');
            if ($data['order_status'] == 10) {
                $map['price'] = $data['price'];
                $map['status'] = 1;
                $map['refund_time'] = time();
                $map['type'] = $data['refund_type'];
                $map['message'] = $data['message'];
                if ($data['price']==0) fail('退款金额不能为空');
            }else if ($data['order_status'] == 11){
                $map['status'] = 2;
                $map['price'] = $data['price'];
                $map['type'] = $data['refund_type'];
                $map['message'] = $data['message'];
            }else{
                $map['status'] = 3;
                $map['type'] = $data['refund_type'];
            }

            // 提交数据
            $dispose = (new MemberRefundModel())->allowField(true)->isUpdate(true, ['id' => $data['refund_id']])->save($map);
            if ($dispose||$dispose==0) {
                win('操作成功', Url::build('MemberRefund/index'));
            }

            fail('操作失败');

        }

        $item = Db::view('member_order', '*')
            ->view('shop', 'member_id as user_id,title', 'shop.member_id = member_order.shop_id')
            ->view('member', 'id as user_id,username', 'member.id = shop.member_id')
            ->where(['member_order.id' => $id])
            ->order(['create_time' => 'desc'])
            ->find();
        $item['buyers'] = Db::name('member')->where('id', $item['member_id'])->value('username');


        $goods_list = (new MemberOrderAttachedModel)->where(['order_id' => $id])->select();


        $refund_find = (new MemberRefundModel)->where(['order_number' => $item['shop_order_number']])->find();


        return $this->fetch('', [
            "mate_title"  => '订单详情',
            'item'        => $item,
            'goods_list'  => $goods_list,
            'refund_find' => $refund_find,

        ]);
    }

}